package com.dq.health.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dq.health.dto.request.role.QueryRoleListRequest;
import com.dq.health.dto.request.role.SaveRoleRequest;
import com.dq.health.dto.response.RoleInfoResponse;
import com.dq.health.entity.menu.SysMenu;
import com.dq.health.entity.role.SysRole;
import com.dq.health.service.IAPIMenuService;
import com.dq.health.service.IAPIRoleService;
import com.dq.health.utils.SecurityUtils;
import com.dq.health.utils.view.StringView;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/role")
@Api(tags = "角色管理")
public class RoleController {

    private @Autowired IAPIRoleService roleService;
    private @Autowired IAPIMenuService menuService;

    @PostMapping(value = "/getAllEnableRole")
    @ApiOperation("获取所有可用的角色")
    public List<SysRole> getAllEnableRole() {
        return roleService.getAllEnableRole();
    }

    @PostMapping(value = "/getRoleList")
    @ApiOperation("查询角色列表")
    public IPage<SysRole> getRoleList(@RequestBody QueryRoleListRequest req) {
        return roleService.getRoleList(req);
    }

    @PostMapping(value = "/getRoleDetail")
    @ApiOperation("查询角色详情")
    public RoleInfoResponse getRoleDetail(@RequestBody QueryRoleListRequest req) {

        Long roleId = req.getRoleId();
        SysRole sysRole = roleService.getSysRole(roleId);

        RoleInfoResponse resp = new RoleInfoResponse();
        resp.setRoleId(sysRole.getRoleId());
        resp.setRoleName(sysRole.getRoleName());
        resp.setRoleKey(sysRole.getRoleKey());
        resp.setStatus(sysRole.getStatus());
        resp.setRemark(sysRole.getRemark());

        List<SysMenu> menus = menuService.selectMenuList();
        resp.setMenus(menuService.buildMenus(menus));

        return resp;
    }

    @PostMapping(value = "/addRole")
    @ApiOperation("新增角色")
    public StringView addRole(@RequestBody SaveRoleRequest req) {
        StringView view = new StringView();

        SysRole sysRole = new SysRole();
        sysRole.setRoleName(req.getRoleName());
        sysRole.setRoleKey(req.getRoleKey());
        sysRole.setStatus(req.getStatus());
        sysRole.setRemark(req.getRemark());
        sysRole.setCreateUserId(SecurityUtils.getLoginUserId());
        sysRole.setUpdateUserId(SecurityUtils.getLoginUserId());
        roleService.saveRole(sysRole, req.getMenuIds());

        view.success("新增成功");
        return view;
    }

    @PostMapping(value = "/updateRole")
    @ApiOperation("修改角色")
    public StringView updateRole(@RequestBody SaveRoleRequest req) {
        StringView view = new StringView();
        Long roleId = req.getRoleId();
        if (roleId == 1L) {
            view.fail("系统管理员无法被编辑");
            return view;
        }

        SysRole sysRole = new SysRole();
        sysRole.setRoleId(roleId);
        sysRole.setRoleName(req.getRoleName());
        sysRole.setRoleKey(req.getRoleKey());
        sysRole.setRemark(req.getRemark());
        sysRole.setUpdateUserId(SecurityUtils.getLoginUserId());
        roleService.updateRole(sysRole, req.getMenuIds());

        view.success("修改成功");
        return view;
    }

    @PostMapping(value = "/updateRoleStatus")
    @ApiOperation("修改角色状态")
    public StringView updateRoleStatus(@RequestBody SaveRoleRequest req) {
        StringView view = new StringView();
        Long roleId = req.getRoleId();
        if (roleId == 1L) {
            view.fail("系统管理员无法被编辑");
            return view;
        }

        SysRole sysRole = new SysRole();
        sysRole.setRoleId(roleId);
        sysRole.setStatus(req.getStatus());
        sysRole.setUpdateUserId(SecurityUtils.getLoginUserId());
        roleService.updateRoleBaseInfo(sysRole);

        view.success("修改成功");
        return view;
    }
}
